package com.xoa.dev.formSign.serivce;

import com.xoa.dao.users.UsersMapper;
import com.xoa.dev.formSign.dao.FormSignMapper;
import com.xoa.dev.formSign.model.FormSign;
import com.xoa.model.department.Department;
import com.xoa.model.flowRunFeedback.FlowRunFeedback;
import com.xoa.model.users.Users;
import com.xoa.service.department.DepartmentService;
import com.xoa.util.AjaxJson;
import com.xoa.util.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
@Service
public class FormSignServiceImpl {

    @Autowired
    FormSignMapper formSignMapper;

    @Autowired
    UsersMapper usersMapper;

    @Autowired
    DepartmentService departmentService;


    public AjaxJson getSignFeedBack( Integer runId, String PRCS_ID, String FLOW_PRCS) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            List<FormSign> result = new ArrayList<FormSign>();
            FormSign formSign = null;
            if (!StringUtils.checkNull(PRCS_ID)) {
                String[] prcsIdArray = PRCS_ID.split(",");
                for (int i = 0, len = prcsIdArray.length; i < len; i++) {
                    formSign = new FormSign();
                    Integer prcsId = Integer.parseInt(prcsIdArray[i]);
                    formSign.setPrcsId(prcsId);
                    List<FlowRunFeedback> list = getSignFeedBack(runId, prcsId, null);
                    for (FlowRunFeedback flowRunFeedback : list) {
                        List<Department> l = departmentService.getFatherDept(flowRunFeedback.getDeptId(), new ArrayList<Department>());
                        StringBuffer sb = new StringBuffer();
                        for (int j = l.size() - 1; j >= 0; j--) {
                            sb.append(l.get(j).getDeptName());
                            if (i > 0) {
                                sb.append("/");
                            }
                        }
                        flowRunFeedback.setLongDeptName(sb.toString());
                        String endTime = flowRunFeedback.getEditTime();
                        if (endTime.indexOf('.')>0){
                            flowRunFeedback.setEditTime(endTime.substring(0, endTime.indexOf('.')));
                        }
                    }
                    formSign.setList(list);
                    result.add(formSign);
                }
            }
            if (!StringUtils.checkNull(FLOW_PRCS)) {
                String[] flowPrcsArray = FLOW_PRCS.split(",");
                for (int j = 0, len = flowPrcsArray.length; j < len; j++) {
                    formSign = new FormSign();
                    Integer flowPrcs = Integer.parseInt(flowPrcsArray[j]);
                    formSign.setFlowPrcs(flowPrcs);
                    List<FlowRunFeedback> list = getSignFeedBack(runId, null, flowPrcs);
                    for (FlowRunFeedback flowRunFeedback : list) {
                        List<Department> l = departmentService.getFatherDept(flowRunFeedback.getDeptId(), new ArrayList<Department>());
                        StringBuffer sb = new StringBuffer();
                        for (int i = l.size() - 1; i >= 0; i--) {
                            sb.append(l.get(i).getDeptName());
                            if (i > 0) {
                                sb.append("/");
                            }
                        }
                        flowRunFeedback.setLongDeptName(sb.toString());
                        String endTime = flowRunFeedback.getEditTime();
                        if (endTime.indexOf('.')>0){
                            flowRunFeedback.setEditTime(endTime.substring(0, endTime.indexOf('.')));
                        }
                    }
                    formSign.setList(list);
                    result.add(formSign);
                }
            }
            ajaxJson.setObj(result);
            ajaxJson.setFlag(true);
        } catch (Exception e) {
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setFlag(false);
        }
        return ajaxJson;
    }

    List<FlowRunFeedback> getSignFeedBack(Integer runId, Integer PRCS_ID, Integer FLOW_PRCS) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("runId", runId);
        map.put("PRCS_ID", PRCS_ID);
        map.put("FLOW_PRCS", FLOW_PRCS);
        return formSignMapper.getSignFeedBack(map);
    }
}
